package dao.impl;

import bean.dto.LogQueryPage;
import bean.dto.PermissionQueryPage;
import bean.pojo.Permissions;
import bean.vo.LogsVO;
import commons.jdbc.basic.JDBCUtil;
import dao.LogDao;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * dao.impl
 * User: mcw
 * Date: 2024/12/19 下午2:13
 * motto:   春天（spring）没有虫子（bug)
 * Description:
 * Version: V1.0
 */
public class LogDaoImpl implements LogDao {
    /**
     * 查询记录数
     * @param logQueryPage
     * @return
     */
    @Override
    public Long findByTotal(LogQueryPage logQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select  count(*) ct  from  system_logs sl left join user u on sl.user_id=u.user_id  where  1=1 ");
        //集合参数
        List<Object> parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(logQueryPage.getOperationType()!=null){
            parameters.add(logQueryPage.getOperationType());
            sql.append(" and  sl.operation_type like concat('%',?,'%') ");
        }
        if(logQueryPage.getUserName()!=null){
            parameters.add(logQueryPage.getUserName());
            sql.append(" and  u.user_name like concat('%',?,'%') ");
        }
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(),parameters.toArray());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //该变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    /**
     * 分页查询
     * @param logQueryPage
     * @return
     */
    @Override
    public List<LogsVO> findByPage(LogQueryPage logQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select  sl.*,u.user_name from  system_logs sl left join user u on sl.user_id=u.user_id  where  1=1  ");
        //集合参数
        List<Object>  parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(logQueryPage.getOperationType()!=null){
            parameters.add(logQueryPage.getOperationType());
            sql.append(" and  sl.operation_type like concat('%',?,'%') ");
        }
        if(logQueryPage.getUserName()!=null){
            parameters.add(logQueryPage.getUserName());
            sql.append(" and  u.user_name like concat('%',?,'%') ");
        }
        sql.append("  order by  sl.log_id desc");
        sql.append("  limit  "+((logQueryPage.getPage()-1)*logQueryPage.getLimit())
                +","+logQueryPage.getLimit());
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
        System.out.println(Arrays.toString(query.toArray()));
        if(query.size()>0){
            List<LogsVO> list=new ArrayList<>();
            for (Map<String, Object> row : query) {//Community
                LogsVO vo=new LogsVO();
                vo.setLogId((Integer) row.get("log_id"));
                vo.setUserName((String) row.get("user_name"));
                vo.setOperationType((String) row.get("operation_type"));
                vo.setOperationTime((LocalDateTime) row.get("operation_time"));
                vo.setOperationDetails((String) row.get("operation_details"));
                list.add(vo);
            }
            System.out.println("List<LogsVO>:list:"+list);
            return  list;
        }
        return null;
    }

    /**
     * 根据id删除日志
     * @param logId
     * @return
     */
    @Override
    public boolean deleteById(Integer logId) {
        String sql="delete from system_logs where log_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql, logId);
        System.out.println("sql:"+sql);
        return update > 0;
    }

    @Override
    public boolean deleteAllLogs() {
        String sql="delete from system_logs";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql);
        System.out.println("sql:"+sql);
        return update > 0;
    }

    @Override
    public void insert(Integer userId,String userName,String operationType, LocalDateTime operationTime) {
        String sql="insert into system_logs(user_id,operation_type,operation_time,operation_details) values(?,?,?,?)";
        JDBCUtil jdbcUtil=new JDBCUtil();
        String operationDetails="用户"+userName+"在"+operationTime+"时，进行了"+operationType+"操作";
        System.out.println("operationDetails："+operationDetails);
        jdbcUtil.update(sql,userId,operationType,operationTime,operationDetails);

    }
}
